package com.bdsys.action;

import java.util.ArrayList;
import java.util.List;

import com.bdsys.common.action.BasicAction;
import com.bdsys.common.constants.Constants;
import com.bdsys.service.DeviceService;
import com.bdsys.servicedto.DeviceDetailDTO;
import com.bdsys.servicedto.DeviceServiceDTO;
import com.bdsys.vo.DeviceVo;

@SuppressWarnings("serial")
public class DeviceAction extends BasicAction implements Constants{
	
	DeviceVo deviceVo;
	
	public String add(){
		DeviceService  deviceService = (DeviceService)getService(DEVICE_SERVICE);
		DeviceDetailDTO inDetailDTO = new DeviceDetailDTO();
		inDetailDTO.setDid(deviceVo.getDid());
		inDetailDTO.setDname(deviceVo.getDname());
		inDetailDTO.setDstatus(deviceVo.getDstatus());
		inDetailDTO.setClassification(deviceVo.getClassification());
		DeviceServiceDTO inServiceDTO = new DeviceServiceDTO();
		inServiceDTO.setDeviceDetailDTO(inDetailDTO);
		deviceService.addDevice(inServiceDTO);
		return "addSuccess";
	}
	
	public String selectAll(){
		DeviceService  deviceService = (DeviceService)getService(DEVICE_SERVICE);
		DeviceServiceDTO outServiceDTO = deviceService.selectAll();
		
		List<DeviceVo> viewList = new ArrayList<DeviceVo>();
		List<DeviceDetailDTO> outList = outServiceDTO.getList();
		for(DeviceDetailDTO deviceDetailDTO : outList){
			DeviceVo deviceVo = new DeviceVo();
			deviceVo.setDid(deviceDetailDTO.getDid());
			deviceVo.setDname(deviceDetailDTO.getDname());
			deviceVo.setDstatus(deviceDetailDTO.getDstatus());
			deviceVo.setClassification(deviceDetailDTO.getClassification());
			viewList.add(deviceVo);
		}
		
		getRequest().put("deviceList", viewList);
		
		return "selectAll";
	
	}
	
	public String selectByConditions(){
		getRequest().put("condition-did", deviceVo.getDid());
		getRequest().put("condition-dname", deviceVo.getDname());
		getRequest().put("condition-dstatus", deviceVo.getDstatus());
		getRequest().put("condition-classification", deviceVo.getClassification());
		DeviceService  deviceService = (DeviceService)getService(DEVICE_SERVICE);
		DeviceDetailDTO inDetailDTO = new DeviceDetailDTO();
		inDetailDTO.setDid(deviceVo.getDid());
		inDetailDTO.setDname(deviceVo.getDname());
		inDetailDTO.setDstatus(deviceVo.getDstatus());
		inDetailDTO.setClassification(deviceVo.getClassification());
		DeviceServiceDTO inServiceDTO = new DeviceServiceDTO();
		inServiceDTO.setDeviceDetailDTO(inDetailDTO);
		DeviceServiceDTO outServiceDTO = deviceService.selectByConditions(inServiceDTO);
		
		List<DeviceVo> viewList = new ArrayList<DeviceVo>();
		List<DeviceDetailDTO> outList = outServiceDTO.getList();
		for(DeviceDetailDTO deviceDetailDTO : outList){
			DeviceVo deviceVo = new DeviceVo();
			deviceVo.setDid(deviceDetailDTO.getDid());
			deviceVo.setDname(deviceDetailDTO.getDname());
			deviceVo.setDstatus(deviceDetailDTO.getDstatus());
			deviceVo.setClassification(deviceDetailDTO.getClassification());
			viewList.add(deviceVo);
		}
		
		getRequest().put("deviceList", viewList);
		
		return "selectAll";
	}
	
	public void setDeviceVo(DeviceVo deviceVo){
		this.deviceVo = deviceVo;
	}
	
	public DeviceVo getDeviceVo(){
		return this.deviceVo;
	}

}
